package com.glp.work.flow.controller;

import com.github.pagehelper.PageInfo;
import com.glp.common.reponse.BaseResponse;
import com.glp.common.reponse.BaseResult;
import com.glp.work.flow.constant.APICode;
import com.glp.work.flow.dto.ProcessAndModelVo;
import com.glp.work.flow.dto.SendEmailSituationDTO;
import com.glp.work.flow.dto.WfPlatformDefDTO;
import com.glp.work.flow.mapper.WfPlatformDefMapper;
import com.glp.work.flow.pojo.WfPlatformDef;
import com.glp.work.flow.pojo.WfPlatformDefModle;
import com.glp.work.flow.response.PageInfoResponse;
import com.glp.work.flow.service.WfPlatformDefModleService;
import com.glp.work.flow.service.WfPlatformDefService;
import com.glp.work.flow.workflow.plugin.flowable.service.FlowProcessDefinitionService;
import com.glp.work.flow.workflow.plugin.flowable.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = {"平台定义Controller"})
@Slf4j
@RequestMapping("/platform")
@RestController
public class WfPlatformDefController {

    @Resource
    private WfPlatformDefService wfPlatformDefService;
    @Resource
    private WfPlatformDefModleService wfPlatformDefModleService;
    @Resource
    private FlowProcessDefinitionService flowProcessDefinitionService;
    @Resource
    private WfPlatformDefMapper wfPlatformDefMapper;

    /**
     * 应用授权
     *
     * @param platformIds 平台定义platform_id集合
     * @param modelKey    流程定义模板的key
     * @return
     */
    @PostMapping(value = "/authorization")
    @ApiOperation(value = "应用授权")
    public BaseResult insertForm(@RequestParam(value = "platformIds", required = false) List<String> platformIds, @RequestParam(value = "modelKey") String modelKey,
                                 @RequestHeader String userNo) {
        wfPlatformDefService.authorization(platformIds, null,null, modelKey, userNo);
        return new BaseResult<>(APICode.SUCCESS.getCode(), "应用授权成功！" + modelKey);
    }

    /**
     * 应用授权情况
     *
     * @return
     */
    @GetMapping(value = "/wpfDefinsertFormCase")
    @ApiOperation(value = "应用授权情况")
    public BaseResult wpfDefinsertFormCase() {
        log.info("查询应用授权列表");
        List<WfPlatformDefDTO> wfPlatformDefDTOList = wfPlatformDefService.wpfDefinsertFormCase();
        return BaseResponse.success(wfPlatformDefDTOList);
    }

    /**
     * 平台应用列表
     *
     * @return
     */
    @GetMapping(value = "/wfPlatformDefList")
    @ApiOperation(value = "平台应用列表")
    public BaseResult wfPlatformDefList(@RequestParam(name = "platformName", required = false) String platformName, @RequestParam(name = "type", required = false) String type) {
        List<WfPlatformDef> wfPlatformDefList = wfPlatformDefService.wfPlatformDefList(platformName, type);
        return BaseResponse.success(wfPlatformDefList);
    }

    @GetMapping("/queryAPPProcess")
    @ApiOperation(value = "查询应用对应的流程")
    public BaseResult<PageInfo<ProcessAndModelVo>> queryAPPProcess(@RequestParam String appId, Integer pageSize, Integer pageNum) {
        log.info("查询应用对应的流程");
        PageInfo<ProcessAndModelVo> processAndModelVoPageInfo = flowProcessDefinitionService.queryAPPProcess(appId, pageSize, pageNum);
        List<ProcessAndModelVo> list = processAndModelVoPageInfo.getList();
        List<ProcessAndModelVo> processAndModelVoList = new ArrayList<>();
        for (ProcessAndModelVo processAndModelVo : list) {
            Long avgTime = wfPlatformDefMapper.findKeyOrder(processAndModelVo.getKey(), appId);
            String distanceTime = "";
            if (avgTime != null) {
                distanceTime = DateUtil.getDistanceTime(avgTime);
            }
            processAndModelVo.setAvgTime(distanceTime);
            processAndModelVoList.add(processAndModelVo);
        }
        processAndModelVoPageInfo.setList(processAndModelVoList);
        return BaseResponse.success(processAndModelVoPageInfo);
    }

    /**
     * 各个平台Email发送情况
     *
     * @return
     */
    @GetMapping(value = "/getSendEmailSituation")
    @ApiOperation(value = "获取各个平台Email发送情况")
    public BaseResult<PageInfoResponse<SendEmailSituationDTO>> getSendEmailSituation(@RequestParam int pageNum, @RequestParam int pageSize) {
        log.info("获取各个平台Email发送情况");
        PageInfo<SendEmailSituationDTO> pageInfo = wfPlatformDefService.getSendEmailSituation(pageNum, pageSize);
        PageInfoResponse<SendEmailSituationDTO> response = new PageInfoResponse<>();
        response.setPageNum(pageInfo.getPageNum());
        response.setPageSize(pageInfo.getPageSize());
        response.setSize(pageInfo.getPages());
        response.setTotal(pageInfo.getTotal());
        response.setList(pageInfo.getList());
        return new BaseResult<>(APICode.SUCCESS.getCode(), "获取各个平台Email发送情况", response);
    }

    /**
     * 各个平台Email发送情况
     *
     * @return
     */
    @PostMapping(value = "/updateSendEmailSituation")
    @ApiOperation(value = "打开或关闭Email通知")
    public BaseResult sendEmailSituation(@RequestBody SendEmailSituationDTO sendEmailSituationDTO) {
        log.info("打开或关闭Email通知,{}", sendEmailSituationDTO);
        wfPlatformDefService.updateSendEmailSituation(sendEmailSituationDTO);
        return new BaseResult<>(APICode.SUCCESS.getCode(), "修改Email通知成功！" + sendEmailSituationDTO);
    }

    /**
     * 修改流程对应mq信息
     *
     * @return
     */
    @PostMapping(value = "/updateModelKeyMQ")
    @ApiOperation(value = "修改流程对应mq信息")
    public BaseResult updateModelKeyMQ(@RequestBody WfPlatformDefModle wfPlatformDefModle) {
        log.info("修改流程对应mq信息,{}", wfPlatformDefModle);
        wfPlatformDefModleService.updateModelKeyMQ(wfPlatformDefModle);
        return new BaseResult<>(APICode.SUCCESS.getCode(), "修改流程对应mq信息！" + wfPlatformDefModle);
    }

}
